using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;

/// <summary>
/// 自动添加 Image，并在指针进入/离开/点击时派发事件。
/// 适用于 UGUI 系统（Canvas + GraphicRaycaster + EventSystem）。
/// </summary>
[RequireComponent(typeof(Image))]
public class UIHoverPropagationHandler : MonoBehaviour,
    IPointerEnterHandler,
    IPointerExitHandler,
    IPointerClickHandler
{
    [System.Serializable]
    public class HoverEvent : UnityEvent<GameObject> { }

    [Header("Hover Events")]
    [Tooltip("当鼠标指针进入 Image 区域时触发")]
    public HoverEvent onHoverEnter;
    [Tooltip("当鼠标指针离开 Image 区域时触发")]
    public HoverEvent onHoverExit;

    [Header("Click Event")]
    [Tooltip("当在 Image 区域内点击（左键）时触发")]
    public HoverEvent onClick;

    void Reset()
    {
        // 确保有 Image 组件
        GetComponent<Image>().raycastTarget = true;
    }

    /// <summary>
    /// 当指针进入该 UI 元素时被调用
    /// </summary>
    public void OnPointerEnter(PointerEventData eventData)
    {
        
        onHoverEnter?.Invoke(gameObject);
    }

    /// <summary>
    /// 当指针离开该 UI 元素时被调用
    /// </summary>
    public void OnPointerExit(PointerEventData eventData)
    {
        onHoverExit?.Invoke(gameObject);
    }

    /// <summary>
    /// 当点击该 UI 元素时被调用
    /// </summary>
    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
            onClick?.Invoke(gameObject);
    }
}